package com.hxd.otc.module.fiat_deal.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.api.Constant;
import com.hxd.otc.base.BaseAdapter;
import com.hxd.otc.module.fiat_deal.bean.DealResultDataBean;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.utils.StringUtil;
import com.hxd.otc.utils.UIUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/9/17 0017.
 * description ${交易列表}
 */
public class DealAdapter extends BaseAdapter<DealAdapter.DealHolder, DealResultDataBean.Data> {

    private int mDealType;

    public DealAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindHolder(@NonNull DealHolder holder, final int position) {

        if (position == getItemCount() - 1) {
            holder.viewBottom.setVisibility(View.VISIBLE);
        } else {
            holder.viewBottom.setVisibility(View.GONE);
        }
        LogUtils.e("===pos: " + position + " , getItemCount : " + getItemCount());
        final DealResultDataBean.Data item = getItem(position);
        holder.ivBankCard.setVisibility(View.GONE);
        holder.ivAlipay.setVisibility(View.GONE);

        String nickName = StringUtil.emptyIfNull(item.getNickName());
        String shortName = "";
        if (nickName.length() > 0) {
            shortName = nickName.substring(0, 1);
        }
        holder.tvShortName.setText(shortName);
        holder.tvBusinessName.setText(nickName);
        switch (mDealType) {
            case Constant.Deal.TYPE_BUY:
                holder.tvType.setText(R.string.order_buy);
                holder.tvType.setBackgroundResource(R.drawable.selector_deal_buy_view);
                break;
            case Constant.Deal.TYPE_SELL:
                holder.tvType.setText(R.string.order_sale);
                holder.tvType.setBackgroundResource(R.drawable.selector_deal_sell_view);
                break;
        }

        holder.tvMoney.setText(StringUtil.formatNumberRetainZero(Double.valueOf(mData.get(position).price), 2, false));
        holder.tvMoneyUnit.setText(item.supportCurrencyCode);

        holder.tvQuota.setText(String.format("%s-%s %s", StringUtil.handleZero(String.valueOf(item.minMoney)), StringUtil.handleZero(String.valueOf(item.maxMoney)), item.supportCurrencyCode));

        BigDecimal volumeDecimal = new BigDecimal(item.volume);
        BigDecimal lockVolume = new BigDecimal(item.lockVolume);
        BigDecimal successVolume = new BigDecimal(item.successVolume);
        String result = volumeDecimal.subtract(lockVolume).subtract(successVolume).setScale(6, RoundingMode.DOWN).toPlainString();

        holder.tvNumber.setText(UIUtils.getContext().getString(R.string.two_placeholder1, result, item.symbol));

        //类型，1微信2支付宝3银行卡
        String supportPay = StringUtil.emptyIfNull(item.supportPay);
        if (TextUtils.isEmpty(supportPay)) {
            holder.ivAlipay.setVisibility(View.GONE);
            holder.ivWeiXin.setVisibility(View.GONE);
            holder.ivBankCard.setVisibility(View.GONE);
        } else {//1微信2支付宝3银行卡
            holder.ivAlipay.setVisibility(supportPay.contains("2") ? View.VISIBLE : View.GONE);
            holder.ivWeiXin.setVisibility(supportPay.contains("1") ? View.VISIBLE : View.GONE);
            holder.ivBankCard.setVisibility(supportPay.contains("3") ? View.VISIBLE : View.GONE);
        }

        holder.tvType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnTransactionListener == null) return;
                mOnTransactionListener.onTransaction(position, item, mDealType);
            }
        });

    }

    private OnTransactionListener mOnTransactionListener;

    public void setOnTransactionListener(OnTransactionListener onTransactionListener) {
        mOnTransactionListener = onTransactionListener;
    }

    public interface OnTransactionListener {
        void onTransaction(int position, DealResultDataBean.Data item, int dealType);
    }

    @NonNull
    @Override
    public DealHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new DealHolder(mInflater.inflate(R.layout.item_deal_view, parent, false));
    }


    static class DealHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_business_name)
        TextView tvBusinessName;
        @BindView(R.id.tv_short_name)
        TextView tvShortName;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_money_unit)
        TextView tvMoneyUnit;
        @BindView(R.id.iv_bank_card)
        ImageView ivBankCard;
        @BindView(R.id.iv_alipay)
        ImageView ivAlipay;
        @BindView(R.id.iv_weixin)
        ImageView ivWeiXin;
        @BindView(R.id.tv_number)
        TextView tvNumber;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_quota)
        TextView tvQuota;
        @BindView(R.id.view_bottom)
        View viewBottom;

        DealHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setDealType(int dealType) {
        mDealType = dealType;
    }
}
